﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using visinia.SmartEngine;

namespace visinia.web.admin.pages
{
    public partial class ManageImages : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Populate(Server.MapPath(WebRoots.GetResourcesRoot));
                imagesGrid.DataSource = BindImageList(Directories.Items[0].Value);
                imagesGrid.DataBind();
            }
        }

        private void Populate(string path)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            if (dir.Exists)
            {
                Directories.Items.Add("\\");
                GetDirectories(dir.GetDirectories(), dir.FullName);
            }
        }

        private void GetDirectories(DirectoryInfo[] subDirs, string path)
        {
            foreach (DirectoryInfo subDir in subDirs)
            {
                DirectoryInfo[] subSubDirs;
                Directories.Items.Add(subDir.FullName.Substring(path.Length));
                subSubDirs = subDir.GetDirectories();
                if (subSubDirs.Length != 0)
                {
                    GetDirectories(subSubDirs, path);
                }
            }
        }

        private List<image> BindImageList(string folder)
        {
            string path = Server.MapPath(WebRoots.GetResourcesRoot);
            if (folder != "\\")
                path += folder;
            List<string> files = new List<string>();
            List<image> images = new List<image>();
            files.AddRange(Directory.GetFiles(path, "*.jpg"));
            files.AddRange(Directory.GetFiles(path, "*.gif"));
            files.AddRange(Directory.GetFiles(path, "*.png"));
            files.AddRange(Directory.GetFiles(path, "*.bmp"));
            foreach (string file in files)
            {
                string name = file.Substring((file.LastIndexOf('\\') + 1));
                string fullname = name;
                if (folder != "\\")
                    fullname = folder + "\\" + name;
                image image = new image();
                image.name = name;
                image.path = WebRoots.GetWebRoot + "image.axd?picture=" + fullname;
                images.Add(image);
            }
            return images;
        }

        public class image
        {
            public string path { get; set; }
            public string name { get; set; }
        }
        protected void SelectDirectory_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Directories.SelectedValue))
            {
                imagesGrid.DataSource = BindImageList(Directories.SelectedValue);
                imagesGrid.DataBind();
            }
            else
            {
                imagesGrid.DataSource = BindImageList(Directories.Items[0].Value);
                imagesGrid.DataBind();
            }
        }

        protected void imagesGrid_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string filename = imagesGrid.Rows[index].Cells[1].Text;
                string folder = Directories.SelectedValue;
                string path = Server.MapPath(WebRoots.GetResourcesRoot);
                if (folder != "\\")
                    path += folder;
                File.Delete(path + "\\" + filename);
                imagesGrid.DataSource = BindImageList(Directories.SelectedValue);
                imagesGrid.DataBind();
            }
        }

        protected void imagesGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}